package cn.lf.android.lawfirm.changeapproval;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zhy.autolayout.AutoRelativeLayout;

import cn.lf.android.lawfirm.BaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.changeapproval.controller.ChangeSummaryConroller;
import cn.lf.android.lawfirm.undertake.approval.modle.Summary;
import cn.lf.android.lawfirm.utils.DebugLog;

public class ChangeApprovalActivity extends BaseActivity implements ChangeSummaryConroller.ChangeSummaryCallback, ChangeSummaryConroller.ApprovalCallback {

    /**
     * 返回
     **/
    private ImageButton img_left;
    /**
     * 标题
     **/
    private TextView mtitle;
    /**
     * 案件详情
     **/
    private AutoRelativeLayout detail;
    /**
     * 费用详情
     **/
    private TextView cost_txt;
    private AutoRelativeLayout rl_cost;
    private TextView after_cost_txt;
    private AutoRelativeLayout rl_after_cost;

    /**
     * 备注
     **/
    private TextView remarks;
    /**
     * 意見
     **/
    private EditText opinion;
    private String caseID = "";
    private ChangeSummaryConroller mSummaryConroller;
    private TextView refuse, adopt;
    /**
     * 申请记录编号
     **/
    private String approvesid = "";
    private AutoRelativeLayout rl_opinion;
    private String relationSid;

    @Override
    public Object getContentResView() {
        return R.layout.activity_change_approval;
    }

    @Override
    public void initView() {
        initTitle();
        caseID = getIntent().getStringExtra("caseID");
        relationSid = getIntent().getStringExtra("relationSid");
        DebugLog.e("变更relationSid===" + relationSid);
        DebugLog.e("变更caseID===" + caseID);
        detail = (AutoRelativeLayout) findViewById(R.id.detail);
        detail.setOnClickListener(this);
        rl_cost = (AutoRelativeLayout) findViewById(R.id.rl_cost);
        rl_cost.setOnClickListener(this);
        cost_txt = (TextView) findViewById(R.id.cost_txt);
        remarks = (TextView) findViewById(R.id.remarks);
        opinion = (EditText) findViewById(R.id.opinion);
        refuse = (TextView) findViewById(R.id.refuse);
        refuse.setOnClickListener(this);
        adopt = (TextView) findViewById(R.id.adopt);
        adopt.setOnClickListener(this);
        rl_opinion = (AutoRelativeLayout) findViewById(R.id.rl_opinion);
        after_cost_txt = (TextView) findViewById(R.id.after_cost_txt);
        rl_after_cost = (AutoRelativeLayout) findViewById(R.id.rl_after_cost);
        rl_after_cost.setOnClickListener(this);
    }


    private void initTitle() {
        img_left = (ImageButton) findViewById(R.id.img_left);
        img_left.setVisibility(View.VISIBLE);
        img_left.setOnClickListener(this);
        mtitle = (TextView) findViewById(R.id.eb_tv_title);
        mtitle.setVisibility(View.VISIBLE);
        mtitle.setText("变更合同审批");
    }

    @Override
    public void initData() {
        //滑动退出
//        addSwipeFinishLayout();
        mSummaryConroller = new ChangeSummaryConroller(this, this, this);
        mSummaryConroller.onSummary(relationSid);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.adopt:
                hintKbTwo();
                mSummaryConroller.onApproval(opinion.getText().toString().trim(), approvesid, "1");
                break;
            case R.id.refuse:
                hintKbTwo();
                mSummaryConroller.onApproval(opinion.getText().toString().trim(), approvesid, "2");
                break;
            case R.id.img_left:
                hintKbTwo();
                finish();
                break;
            case R.id.detail:
                hintKbTwo();
                Intent intent = new Intent(ChangeApprovalActivity.this, ChangeCaseDetailActivity.class);
                intent.putExtra("bean", mBean);
                intent.putExtra("caseID", caseID);
                startActivity(intent);
                break;
            case R.id.rl_after_cost:
                hintKbTwo();
                Intent intent1 = new Intent(ChangeApprovalActivity.this, NowCostDetailActivity.class);
                intent1.putExtra("relationID", approvesid);
                startActivity(intent1);
                break;
            case R.id.rl_cost:
                hintKbTwo();
                Intent intent2 = new Intent(ChangeApprovalActivity.this, BeforeCostDetailActivity.class);
                intent2.putExtra("approvesid", approvesid);
                startActivity(intent2);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    Summary mBean;

    @Override
    public void onChangeSummaryuccessed(Summary beans) {
        mBean = beans;
        DebugLog.e("777" + mBean);
        approvesid = beans.id;
        setUI(beans);
    }

    @Override
    public void onChangeSummaryFailed() {

    }

    private void setUI(Summary beans) {
        cost_txt.setText(beans.historyContractAmount);
        remarks.setText(beans.ApplyForOpinion);
        after_cost_txt.setText(beans.contractAmount);
        if ("y".equals(beans.isAllow)) {
            rl_opinion.setVisibility(View.VISIBLE);
            refuse.setVisibility(View.VISIBLE);
            adopt.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onApprovalsccessed(String beans) {
        ChangeApprovalListActivity.isRefresh = true;
        showCToast("操作成功");
        finish();
    }

    @Override
    public void onApprovalFailed() {

    }
}
